local skel = fk.CreateSkill {
  name = "emo__qiting",
}

Fk:loadTranslationTable{
  ["emo__qiting"] = "栖庭",
  [":emo__qiting"] = "当你造成或受到伤害后，你可以摸一张牌，若受伤角色：不为女性，你翻面；手牌数与你不同，你须先重铸一张锦囊牌。",

  ["#emo__qiting-draw"] = "栖庭：你可以摸一张牌",
  ["#emo__qiting-draw-turnover"] = "栖庭：你可以摸一张牌并翻面",
  ["#emo__qiting-recast"] = "栖庭：你可以重铸一张锦囊牌，摸一张牌",
  ["#emo__qiting-recast-turnover"] = "栖庭：你可以重铸一张锦囊牌，摸一张牌，并翻面",

  ["$emo__qiting1"] = "处尊居显，位极椒房。",
  ["$emo__qiting2"] = "自在东宫，及即尊位。",
}

skel:addEffect(fk.Damage, {
  anim_type = "drawcard",
  can_trigger = function (self, event, target, player, data)
    return target == player and player:hasSkill(skel.name) and data.to
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local cards = {}
    local recast = data.to:getHandcardNum() ~= player:getHandcardNum()
    local prompt = recast and "#emo__qiting-recast" or "#emo__qiting-draw"
    if not data.to:isFemale() then
      prompt = prompt .. "-turnover"
    end
    if recast then
      cards = room:askToCards(player, {
        min_num = 1, max_num = 1, pattern = ".|.|.|.|.|trick", skill_name = skel.name, prompt = prompt,
      })
      if #cards == 0 then return false end
    elseif not room:askToSkillInvoke(player, {skill_name = skel.name, prompt = prompt}) then
      return false
    end
    event:setCostData(self, {cards = cards})
    return true
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local cards = event:getCostData(self).cards
    if cards and #cards > 0 then
      room:recastCard(cards, player, skel.name)
    end
    if player.dead then return end
    player:drawCards(1, skel.name)
    if not player.dead and not data.to:isFemale() then
      player:turnOver()
    end
  end,
})

return skel
